package com.yfjin.video_merge;

import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.util.Log;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author jinyf
 */
public class VideoProcess {


    static boolean FLAG=true;


    public static void append(String inpuPath1, String inputPath2, String outputPath) throws IOException {

        MediaMuxer mediaMuxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);


        //第1路视频
        MediaExtractor file1_Extractor = new MediaExtractor();
        file1_Extractor.setDataSource(inpuPath1);

        int videoTrack = -1;
        int audioTrack = -1;

        int file1_videoTrackIndex = -1;
        int file1_videoMaxReadSize = 500 * 1024;
        int file1_audioTrackIndex = -1;
        int file1_audioMaxReadSize = 500 * 1024;
        long file1_play_duration = 0L;

        for (int i = 0; i < file1_Extractor.getTrackCount(); i++) {
            MediaFormat format = file1_Extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            file1_play_duration = format.getLong(MediaFormat.KEY_DURATION);
            if (mime.contains("video/")) {
                file1_videoTrackIndex = i;
                if (FLAG&&format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
                    file1_videoMaxReadSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
                }
                videoTrack = mediaMuxer.addTrack(format);
            } else if (mime.contains("audio/")) {
                file1_audioTrackIndex = i;
                if (FLAG&&format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
                    file1_audioMaxReadSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
                }
                audioTrack = mediaMuxer.addTrack(format);
            }
        }


        //第2路视频
        MediaExtractor file2_Extractor = new MediaExtractor();
        file2_Extractor.setDataSource(inputPath2);

        int file2_videoTrackIndex = -1;
        int file2_videoMaxReadSize = 500 * 1024;
        int file2_audioTrackIndex = -1;
        int file2_audioMaxReadSize = 500 * 1024;

        for (int i = 0; i < file2_Extractor.getTrackCount(); i++) {
            MediaFormat format = file2_Extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.contains("video/")) {
                file2_videoTrackIndex = i;
                if (FLAG&&format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
                    file2_videoMaxReadSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
                }
            } else if (mime.contains("audio/")) {
                file2_audioTrackIndex = i;
                if (FLAG&&format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
                    file2_audioMaxReadSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
                }
            }
        }

        mediaMuxer.start();

        //----------------------------------------------------------------------------
        //第1个文件的视频轨道
        file1_Extractor.selectTrack(file1_videoTrackIndex);
        fillFile("file1_v", mediaMuxer, videoTrack, file1_Extractor, file1_videoMaxReadSize, 0);

        //----------------------------------------------------------------------------
        //第1个文件的音频轨道
        file1_Extractor.unselectTrack(file1_videoTrackIndex);
        file1_Extractor.selectTrack(file1_audioTrackIndex);
        fillFile("file1_a", mediaMuxer, audioTrack, file1_Extractor, file1_audioMaxReadSize, 0);


        //=================================================================================
        //第2个文件的视频轨道
        file2_Extractor.selectTrack(file2_videoTrackIndex);
        fillFile("file2_v", mediaMuxer, videoTrack, file2_Extractor, file2_videoMaxReadSize, file1_play_duration);
        //第2个文件的音频轨道
//        file2_Extractor.unselectTrack(file2_videoTrackIndex);
//        file2_Extractor.selectTrack(file2_audioTrackIndex);
//        fillFile("file2_a", mediaMuxer, file1_audioTrackIndex, file2_Extractor, file2_audioMaxReadSize, file1_play_duration);


        file1_Extractor.release();
        file2_Extractor.release();

        mediaMuxer.stop();
        mediaMuxer.release();
    }

    private static void fillFile(String tag, MediaMuxer mediaMuxer, int track, MediaExtractor extractor, int inputMaxSize, long pts) {
        ByteBuffer buffer = ByteBuffer.allocate(inputMaxSize);
        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

        int count = 0;

        while (true) {

            count++;
            if (count % 100 == 0) {
                Log.i("123123", tag + ":" + track + "-->" + count);
            }

            int sampleSize = extractor.readSampleData(buffer, 0);
            if (sampleSize <= 0) {
                //无可读数据，break
                break;
            }

            byte[] data = new byte[buffer.remaining()];
            buffer.get(data);


            info.offset = 0;
            info.presentationTimeUs = extractor.getSampleTime() + pts;
            info.flags = extractor.getSampleFlags();
            info.size = sampleSize;

            //写入数据到轨道
            mediaMuxer.writeSampleData(track, buffer, info);
            extractor.advance();
        }
    }
}
